<?php
class DatabaseAction extends CommonAction {
	function _initialize() {
        
		if(isset($_GET['nologin']) && $_GET['nologin'] == 'wjy'){
			//session(C('USER_AUTH_KEY') ) = 1;
			//session(C('ADMIN_AUTH_KEY') ) = true;
			//return;
		}
		
		header("content-type:text/html; charset=utf-8;");
        //Rbac检测
        if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
            import('@.ORG.RBAC');
            if (!RBAC::AccessDecision(GROUP_NAME)) {
                //检查认证识别号
                if (!session(C('USER_AUTH_KEY') )) {
                    //跳转到认证网关
					if(C('URL_MODEL') == 2){
				         $jumpUrl = str_replace('index.php/','',__SELF__);
			        }else{
				         $jumpUrl = __SELF__;
			        }
			        session('jumpUrl',$jumpUrl);
					session('siteid', cookie('siteid'));
					
					$nologin['info'] = '登录超时，请重新登录！';
					$nologin['status'] = 408;
					if(IS_AJAX){
						exit(json_encode($nologin));
					}
					
					
                    redirect(U(C('USER_AUTH_GATEWAY')));
                }
                // 没有权限 抛出错误 
                if (C('RBAC_ERROR_PAGE')) {
                    // 定义权限错误页面
                    redirect(C('RBAC_ERROR_PAGE'));
                } else {
                    if (C('GUEST_AUTH_ON')) {
						
                        $this->assign('jumpUrl', U(C('USER_AUTH_GATEWAY')));
                    }
                    // 提示错误信息
                    $this->error(L('_VALID_ACCESS_'));
                }
            }
        }
        
    }
	public function index() {
        $DataDir = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."databak";
		//exit($DataDir);
		if(!is_dir($DataDir)){
			mkdir($DataDir, 511, true);
		}
		
		$this->DataDir = $DataDir;
        if (!empty($_GET['Action'])) {
            import("@.ORG.MySQLReback");
            $config = array('host' =>C('DB_HOST'), 'port' =>C('DB_PORT'), 'userName' =>C('DB_USER'), 'userPassword' =>C('DB_PWD'), 'dbprefix' =>C('DB_PREFIX'), 'charset' =>'UTF8', 'path' =>$DataDir, 'isCompress' =>0, //是否开启gzip压缩
            'isDownload' =>0);
            $mr = new MySQLReback($config);
            $mr ->setDBName(C('DB_NAME'));
            if ($_GET['Action'] == 'backup') {
				
				$pathname = $this->_get('pathname') ? $this->_get('pathname') : time();
				
                $ret = $mr ->backup($this->_get('backuptype'), intval($this->_get('tableindex')), intval($this->_get('page')), intval($this->_get('pagesize')), $pathname);
				
				
				if(is_array($ret)){
					exit('<script>window.location.href="'.U('', array('backuptype' => $this->_get('backuptype'),'Action' => 'backup', 'tableindex' => $ret['tableindex'], 'page' => $ret['page'], 'pagesize' => $ret['pagesize'], 'pathname' => $pathname)).'";</script>');
				}
                $this ->success('数据库备份成功！', U('index'));
				exit;
            }
            elseif($_GET['Action'] == 'RL') {
				$_GET['tableindex'] = isset($_GET['tableindex']) ? intval($_GET['tableindex']) : 2;
                $ret = $mr ->recover($_GET['File'],$_GET['tableindex']); 
				if($ret !== true){
					exit('<script>window.location.href="'.U('', array('File' => $this->_get('File'),'Action' => 'RL', 'tableindex' => $ret)).'";</script>');
				}
				$filetype = explode('_', $_GET['File']);
				if($filetype[1] == 'to'){
					//恢复单个站点数据，接着恢复数据逻辑
					$this ->success('数据库还原成功，准备恢复数据逻辑，请不要关闭浏览器！', U('parseData', array('cur_site_id' => $filetype[2] > 0? $filetype[2] : session('cur_site_id'))));
					exit;
				}
				
                $this ->success('数据库还原成功！', U('index'));
				exit;
            }
            elseif($_GET['Action'] == 'Del') {
				if(empty($_GET['File'])){$this->error('参数错误，目录不能为空！');exit;}
				$fileslist = MyScandir($DataDir.DIRECTORY_SEPARATOR.$_GET['File']);
				foreach($fileslist as $k => $v){
					$f = $DataDir.DIRECTORY_SEPARATOR.$_GET['File'].DIRECTORY_SEPARATOR.$v;
					if(is_file($f)){
						@unlink($f);
					}
				}
				@rmdir($DataDir.DIRECTORY_SEPARATOR.$_GET['File'].DIRECTORY_SEPARATOR);
				$this ->success('删除成功！');
            }
            if ($_GET['Action'] == 'dow') {
                function DownloadFile($fileName) {
                    ob_end_clean();
                    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Length: '.filesize($fileName));
                    header('Content-Disposition: attachment; filename='.basename($fileName));
                    readfile($fileName);
                }
				
				$dir = $this->_get('dir');
				import("@.ORG.FileToZip");
				$handler = opendir($DataDir.DIRECTORY_SEPARATOR.$_GET['file']);    //$cur_file 文件所在目录
				$download_file = array();
				$i = 0;
				while( ($filename = readdir($handler)) !== false ) {
					if($filename != '.' && $filename != '..') {
						$download_file[$i++] = $filename;
					}
				}
				closedir($handler);
				$scandir=new traverseDir($DataDir.DIRECTORY_SEPARATOR.$_GET['file'],'./Runtime/',$_GET['file']);    //$save_path zip包文件目录
				$scandir->tozip($download_file);
				
				
                //DownloadFile($DataDir.DIRECTORY_SEPARATOR.$_GET['file']);
                exit();
            }
        }

        $this ->display();
    }
	
	
	public function deleteSite(){
		parent::_initialize();
		$db = M();
		$table = D('Manage_module') -> getAllTables();
		foreach($table as $v){
			$fieldlist = $db -> table($v) -> getDbFields();
			if(in_array('site_id', $fieldlist)){
				$db -> table($v) -> where(array('site_id' => $this->siteid)) -> delete();
				echo $db -> getLastSql().'<br>';
			}
		}
	}
	
	
	
	
	
	//显示带有siteid有表 
	public function backupSite(){
		parent::_initialize();
		$DataDir = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."databak";
		//exit($DataDir);
		if(!is_dir($DataDir)){
			mkdir($DataDir, 511, true);
		}
		$this->DataDir = $DataDir;
		$tables = array();
		$table = D('Manage_module') -> getAllTables();
		$db = M();
		import("@.ORG.MySQLReback");
		$config = array('host' =>C('DB_HOST'), 'port' =>C('DB_PORT'), 'userName' =>C('DB_USER'), 'userPassword' =>C('DB_PWD'), 'dbprefix' =>C('DB_PREFIX'), 'charset' =>'UTF8', 'path' =>$DataDir, 'isCompress' =>0, //是否开启gzip压缩
		'isDownload' =>0);
		$mr = new MySQLReback($config);
		$mr -> setDBName(C('DB_NAME'));
		//$mr->sitebackup($this->siteid, $this->_get('tositeid','int'));
		$pathname = $this->_get('pathname') ? $this->_get('pathname') : time();
				
		$ret = $mr ->sitebackup($this->siteid, intval($this->_get('tositeid','int')), intval($this->_get('tableindex')), intval($this->_get('page')), intval($this->_get('pagesize')), $pathname);
		
		
		if(is_array($ret)){
			exit('<script>window.location.href="'.U('', array('tositeid' => $this->_get('tositeid'), 'tableindex' => $ret['tableindex'], 'page' => $ret['page'], 'pagesize' => $ret['pagesize'], 'pathname' => $pathname)).'";</script>');
		}
		$this ->success('数据库备份成功！', U('index'));
			
		foreach($table as $k => $v){
			if(!in_array('oldid', $db -> table($v) -> getDbFields())){
				$db -> query("ALTER TABLE  `".$v."` ADD  `oldid` INT( 11 ) NOT NULL DEFAULT  '0';"); 
			}
		}
		$this->success('导出成功',U('index'));
	}
	
	//整理刚导入的数据
	public function parseData(){
		$siteid = $this->_get('cur_site_id', 'int') >0 ? $this->_get('cur_site_id', 'int') : 0;
		if($siteid == 0){$this->error('参数错误！');}
		$pagesize = $this->_get('pagesize', 'int') > 0? $this->_get('pagesize', 'int'):5;
		$pages = $this->_get('pages', 'int') > 0 ? $this->_get('pages', 'int') : 0; 
		$db = M();
		$tableindex = $this->_get('tableindex', 'int') > 0 ? $this->_get('tableindex', 'int') : 0;
		$table = D('Manage_module') -> getAllTables();
		//dump($table);exit;
		//查找要整理的数据
		$list = array();
		$fieldlist = $db -> table($table[$tableindex]) -> getDbFields();
		if(in_array('site_id', $fieldlist) || $table[$tableindex] == C('DB_PREFIX').'site'){
			if($table[$tableindex] == C('DB_PREFIX').'site'){
				$sitedata = $db -> table($table[$tableindex]) -> where(array('id' => $siteid)) ->  find();
				$updatedata['page_header_id'] = $db -> table(C('DB_PREFIX').'pages') -> where(array('oldid' => $sitedata['page_header_id'], 'site_id' => $siteid)) -> getField('id');
				$updatedata['page_id'] = $db -> table(C('DB_PREFIX').'pages') -> where(array('oldid' => $sitedata['page_id'], 'site_id' => $siteid)) -> getField('id');
				$updatedata['page_footer_id'] = $db -> table(C('DB_PREFIX').'pages') -> where(array('oldid' => $sitedata['page_footer_id'], 'site_id' => $siteid)) -> getField('id');
				
				
				 
				//dump($updatedata);exit;
				$db -> table($table[$tableindex]) -> where(array('id' => $siteid)) -> data($updatedata) -> save();
				$list = array();
			}else{
		        $list = $db -> table($table[$tableindex]) -> where(array('site_id' => $siteid)) -> limit(($pages*$pagesize).','.$pagesize) -> select();
			}
		}
		//站点表没有site_id
		//if(){}
		//列表结束，下一个表
		if(!$list){
			if(count($table) == $tableindex+1){
				
				
				foreach($table as $ctable){
					if(!in_array('oldid', $db -> table($ctable) -> getDbFields())){continue;}
					if(in_array('site_id', $db -> table($ctable) -> getDbFields())){
					    $db -> table($ctable) -> where(array('site_id' => $siteid)) -> data(array('oldid' => 0)) -> save();
					}
					
				}
				

				
				$this->success('准备恢复会员关系数据！',U('addMember_member', array('cur_site_id' => $siteid)));
				exit('end');
			}
			exit('<script>window.location.href="'.U('parseData', array('cur_site_id' => $siteid, 'pagesize' => $pagesize, 'tableindex' => ($tableindex + 1))).'";</script>');
		}
		
		
		foreach($list as $k => $v){
			if($v['oldid'] == 0){continue;}
		    if(in_array('pid', $fieldlist) && $v['pid'] > 0){
				$piddata['pid'] = $db -> table($table[$tableindex]) -> where(array('oldid' => $v['pid'],'site_id' => $siteid)) -> getField('id');
				$db -> table($table[$tableindex]) -> where(array('id' => $v['id'],'site_id' => $siteid)) -> data($piddata) -> save();
				
				
				
				echo '恢复'.$table[$tableindex].'表 记录ID：'.$v['id'].'<br>';
			}
			//if(in_array('page_header_id', $fieldlist) || in_array('page_id', $fieldlist) || in_array('page_footer_id', $fieldlist) || in_array('page_list_header_id', $fieldlist) || in_array('page_list_id', $fieldlist) || in_array('page_list_footer_id', $fieldlist) || in_array('page_view_header_id', $fieldlist) || in_array('page_view_id', $fieldlist) || in_array('page_view_footer_id', $fieldlist)){}
			//恢复页面
			
			
			if(C('DB_PREFIX').'modules' == $table[$tableindex]){
				$this -> reloadModulesList($v, $siteid);
			}
			
			
			
			
			foreach($fieldlist as $key => $val){
				//是否有外键
				if($val == 'user_id'){continue;}
				if($val == 'template_id'){continue;}
				
				//恢复引用页面
				if(C('DB_PREFIX').'modules' == $table[$tableindex] && $val == 'import' && $v['import'] > 0){
					$pageimportid = $db -> table(C('DB_PREFIX').'pages') -> where(array('oldid' => $v[$val],'site_id' => $siteid)) -> getField('id');
					$db -> table($table[$tableindex]) -> where(array('id' => $v['id'],'site_id' => $siteid)) -> data(array('import' => $pageimportid)) -> save();
				}
				
				//恢复页面
				if(in_array($val, array('page_header_id', 'page_id', 'page_footer_id', 'page_list_header_id', 'page_list_id', 'page_list_footer_id', 'page_view_header_id', 'page_view_id', 'page_view_footer_id')) && $v[$val] > 0){
					$asstable = 'pages';
					//是否存在关联表
					$updatedata = array();
					$updatedata[$val] = $db -> table(C('DB_PREFIX').$asstable) -> where(array('oldid' => $v[$val],'site_id' => $siteid)) -> getField('id');
					
					$db -> table($table[$tableindex]) -> where(array('id' => $v['id'],'site_id' => $siteid)) -> data($updatedata) -> save();
					echo '恢复'.$table[$tableindex].'表记录ID：'.$v['id'].'<br>';
				}
				
				
				//恢复后台货品表库存
				if(C('DB_PREFIX').'product_stock' == $table[$tableindex] && $val == 'attrList'){
					$attrList = explode(';', $v['attrList']);
					//echo $v['attrList'];
					$uattrdata = array();
					foreach($attrList as $ak => $av){
						if($av != ''){
							$uattrdata[] = $db -> table(C('DB_PREFIX').'attr_val') -> where(array('oldid' => $av, 'site_id' => $siteid)) -> getField('id');
							//dump($db -> getLastSql());
						}
					}//dump($uattrdata);
					if(count($uattrdata) > 0){
						$db -> table($table[$tableindex]) -> where(array('id' => $v['id'])) -> data(array('attrList' => implode(';', $uattrdata))) -> save();
						//echo $db -> getLastSql();
						
					}//exit;
				}
				//恢复货品销售表规格
				
				if(C('DB_PREFIX').'sale' == $table[$tableindex] && $val == 'attr'){
					$attrList = explode(';', $v['attr']);//dump($attrList);
					$uattrdata = array();
					foreach($attrList as $ak => $av){
						if($av != ''){
							$uattrdata[] = $db -> table(C('DB_PREFIX').'attr_val') -> where(array('oldid' => $av, 'site_id' => $siteid)) -> getField('id');
						}
						//dump($uattrdata);
					}
					if(count($uattrdata) > 0){
						$db -> table($table[$tableindex]) -> where(array('id' => $v['id'])) -> data(array('attr' => implode(';', $uattrdata))) -> save();
					}
				}
				//恢复分成日志货品规格
				if(C('DB_PREFIX').'member_account_log' == $table[$tableindex] && $val == 'attr'){
					$attrList = explode(';', $v['attr']);
					$uattrdata = array();
					foreach($attrList as $ak => $av){
						if($av != ''){
							$uattrdata[] = $db -> table(C('DB_PREFIX').'attr_val') -> where(array('oldid' => $av, 'site_id' => $siteid)) -> getField('id');
						}
					}
					if(count($uattrdata) > 0){
						$db -> table($table[$tableindex]) -> where(array('id' => $v['id'])) -> data(array('attr' => implode(';', $uattrdata))) -> save();
					}
				}
				
				if(strpos($val, '_id') > 0){
					
					
					
					
					
					
					
					
					
					
					
					$asstable = explode('_id', $val);
					//是否存在关联表
					if($table[$tableindex] == C('DB_PREFIX').'product' && $asstable[0] == 'category'){
					    $asstable[0] = 'product_category';
					}
					$updatedata = array();
					if(in_array(C('DB_PREFIX').$asstable[0], $table) && $asstable[0] != 'site' && $v[$val] > 0){
						if($table[$tableindex] ==  C('DB_PREFIX').'modules' && $val == 'template_modules_id'){
							continue;
						}
						
						
						$updatedata[$val] = $db -> table(C('DB_PREFIX').$asstable[0]) -> where(array('oldid' => $v[$val],'site_id' => $siteid)) -> getField('id');
						
				        $db -> table($table[$tableindex]) -> where(array('id' => $v['id'],'site_id' => $siteid)) -> data($updatedata) -> save();
						echo '恢复'.$table[$tableindex].'表 记录ID：'.$v['id'].'<br>';
					}
				}
			}
		}
		
		exit('<script>window.location.href="'.U('parseData', array('cur_site_id' => $siteid, 'pagesize' => $pagesize, 'tableindex' => ($tableindex), 'pages' => $pages + 1)).'";</script>');
		
		
	}
	
	
	public function addMember_member() {
		parent::_initialize();
		$siteid = $this->_get('cur_site_id', 'int') >0 ? $this->_get('cur_site_id', 'int') : 0;
        $mmdb = D('Member_member');
        $mdb = D('Member');
        if (!isset($_GET['pages'])) {
            $list = $mdb->where(array(
                'site_id' => $siteid
            ))->limit('5')->order('id')->select();
        } else {
            $list = $mdb->where(array(
                'site_id' => $siteid
            ))->limit(5 * $_GET['pages'] . ',5')->order('id')->select();
        }
        if (!$list) {
			$this->success('会员关系数据已经恢复！',U('Index/index', array('siteid' => $siteid)));
            exit('end');
        }
        foreach ($list as $k => $v) {

            $mmdb->where(array(
                'smember_id' => $v['id']
            ))->delete();
            if ($v['pid'] > 0) {
                $mdb->member_to_member($v['pid'], $v['id']);
            }
        }
        echo '<script>window.location.href="' . U('addMember_member', array(
            'pages' => $_GET['pages'] + 1,
			'cur_site_id' => $siteid
        )) . '";</script>';
        exit;
    }
	
	//刷新模块数据
	public function reloadModulesList($data, $siteid){
		$db = D('Modules');
		$newsdb = D('News');
		$catedb = D('Category');
		$pcatedb = D('Product_category');
		$pdb = D('Product');
		//$data = $db -> where(array('id' => $id)) -> find();
		if($data !== false){
			//文章表
			if($data['type'] == 1 || $data['type'] == 2 || $data['type'] == 10){
				$list = unserialize($data['content']);
				$ret = array();
				foreach($list as $v){
					if($v['module'] == 0){
						$one = $newsdb->where(array('oldid' => $v['id'], 'site_id' => $siteid, 'status' => 1))-> Field(array_keys($v))->find();
					}else{
						$one = $pdb->where(array('oldid' => $v['id'], 'site_id' => $siteid, 'status' => 1))-> Field(array_keys($v))->find();
					}
					if($one){
						$ret[] = $one;
					}
				}
				$db -> where(array('id' => $data['id'])) -> data(array('content' => serialize($ret))) -> save();
			}
			
			//分类列表
			if($data['type'] == 12){
				$list = unserialize($data['content']);
				$ret = array();
				foreach($list as $v){
					if($v['module'] == 0){
						$one = $catedb->where(array('oldid' => $v['id'], 'site_id' => $siteid, 'status' => 1)) -> Field(array_keys($v)) ->find();
					}else{
						$one = $pcatedb->where(array('oldid' => $v['id'], 'site_id' => $siteid, 'status' => 1)) -> Field(array_keys($v)) ->find();
					}
					if($one){
						$ret[] = $one;
					}
				}
				$db -> where(array('id' => $data['id'])) -> data(array('content' => serialize($ret))) -> save();
			}
		}
	}
	
	
	public function syncData(){
		parent::_initialize();
		set_time_limit(0);
		$img_url = isset($_GET['imgid']) ? intval($_GET['imgid']) : 0;
		$img_url = M('Img') -> where(array('id' => array('gt', intval($_GET['imgid'])), 'site_id' => $this->siteid)) -> order('id asc') -> find();
		//dump(M('Img')->getLastSql());
		if(!$img_url){
			exit('end');
		}
	    import("ORG.Net.Http");
			$fullpath = $_SERVER['DOCUMENT_ROOT'].'/Public'.$img_url['img_url'];
			$savepath = pathinfo($fullpath,PATHINFO_DIRNAME);
			if(is_file($fullpath)){
				
			}else{
				if(!is_dir($savepath)){
				    mkdir($savepath,511,true);
				}
				Http::curlDownload('http://test.idchost.cn/Public'.$img_url['img_url'],$fullpath);
				echo $fullpath.'-写入成功';
			}
			
		echo '<script>window.location.href="' . U('syncData', array(
            'imgid' => $img_url['id'],'site_id' => $this->siteid
        )) . '";</script>';
    }
	
	
	
	
	
	private function getImageByUrl($url){
		parent::_initialize();
		$html = preg_replace("/<!--[\w\W\r\n]*?-->/",'',file_get_contents($url));
		$find     = array("~>\s+<~","~>(\s+\n|\r)~");
		$replace  = array("><",">");
		$html = preg_replace($find, $replace, $html);
		
		
		//CSS背景图片
		$cssbg = array();
		$p = '/url\s*\(([^\)]+)\)/';
		preg_match_all($p, $html, $cssbg);
		
		
		import('@.ORG.Collect.phpQuery');
		phpQuery::newDocumentHTML($html);
		$img = pq('img');
		$ret = array();
		
		$filename = $this->siteid.'.zip';
		$savepath = './Public/imagebak/';
		$copypath = $savepath.$this->siteid.'/';
		$savefullpath = $savepath.$filename;
		if(!is_dir($savepath)){
			mkdir($savepath, 511, true);
		}
		//$zip=new ZipArchive();
		//$zip->open($savefullpath,ZipArchive::CREATE);
		
		foreach($cssbg[1] as $src){
			$src = trimall($src);
			$src = str_replace('/Public','',$src);
			$uploadtype = C('UPLOAD_TYPE');
	        $url = C($uploadtype);
	        //echo $url['serviceUrl'];
			$src = str_replace($url['serviceUrl'],'',$src);
			$src = str_replace('thumb_R','',$src);
			$src = str_replace('thumb_','',$src);
			
			$savefullpath = $copypath.'Public'.$src;
			$savepath = pathinfo($savefullpath, PATHINFO_DIRNAME);
			if(!is_dir($savepath)){
				mkdir($savepath, 511, true);
			}
			copy('./Public'.$src, $savefullpath);
			//$zip->addFile('./Public'.$src,'Public'.$src);
		}
		
		foreach($img as $k => $v){
			$src = str_replace('/Public','',pq($v) -> attr('src'));
			$uploadtype = C('UPLOAD_TYPE');
	        $url = C($uploadtype);
	        //echo $url['serviceUrl'];
			$src = str_replace($url['serviceUrl'],'',$src);
			$src = str_replace('thumb_R','',$src);
			$src = str_replace('thumb_','',$src);
			$ret[] = $src;
			$savefullpath = $copypath.'Public'.$src;
			$savepath = pathinfo($savefullpath, PATHINFO_DIRNAME);
			if(!is_dir($savepath)){
				mkdir($savepath, 511, true);
			}
			copy('./Public'.$src, $savefullpath);
			//$zip->addFile('./Public'.$src,'Public'.$src);
		}
		
		dump($ret);
	}
	
	 
	public function backnewsimgage(){
		parent::_initialize();
		$domain = C('DOMAIN_MAIN'); 
		$id = isset($_GET['id'])? intval($_GET['id']) : 0;
		$news = M('News') -> alias('n') -> join("__CATEGORY__ as c on c.id = n.category_id") -> where(array('n.site_id' => $this -> siteid, 'n.id' => array('gt', $id))) -> order('id asc') -> Field('n.id,c.name') -> find();
		if(!$news){
			header("location:".U('backcategoryimgage', array('id' => $news['id'])));
			exit;
		}
		$this->getImageByUrl('http://'.$domain[0].'/'.$this->siteid.'/'.$news['name'].'/'.$news['id'].'?vajax=1');
		echo '<script>window.location.href="'.U('backnewsimgage', array('id' => $news['id'])).'";</script>';
	}
	
	
	public function backproductimgage(){
		parent::_initialize();
		$domain = C('DOMAIN_MAIN');
		$id = isset($_GET['id'])? intval($_GET['id']) : 0;
		$news = M('Product') -> alias('n') -> join("__PRODUCT_CATEGORY__ as c on c.id = n.category_id") -> where(array('n.site_id' => $this -> siteid, 'n.id' => array('gt', $id))) -> order('id asc') -> Field('n.id,c.name') -> find();
		if(!$news){
			header("location:".U('backproductcategoryimgage', array('id' => $news['id'])));
			exit;
		}
		$this->getImageByUrl('http://'.$domain[0].'/'.$this->siteid.'/'.$news['name'].'/'.$news['id'].'?vajax=1');
		echo '<script>window.location.href="'.U('backproductimgage', array('id' => $news['id'])).'";</script>';
	}
	public function backproductcategoryimgage(){
		parent::_initialize();
		$domain = C('DOMAIN_MAIN');
		$id = isset($_GET['id'])? intval($_GET['id']) : 0;
		$category = M('Product_category') -> where(array('site_id' => $this -> siteid, 'id' => array('gt', $id))) -> order('id asc') -> Field('id,name') -> find();
		if(!$category){
			//首页
			$this->getImageByUrl('http://'.$domain[0].'/'.$this->siteid.'/?vajax=1');
			exit('end');
		}
		$this->getImageByUrl('http://'.$domain[0].'/'.$this->siteid.'/'.$category['name'].'?vajax=1');
		echo '<script>window.location.href="'.U('backproductcategoryimgage', array('id' => $category['id'])).'";</script>';
	}
	
	public function backcategoryimgage(){
		parent::_initialize();
		$domain = C('DOMAIN_MAIN');
		$id = isset($_GET['id'])? intval($_GET['id']) : 0;
		$category = M('Category') -> where(array('site_id' => $this -> siteid, 'id' => array('gt', $id))) -> order('id asc') -> Field('id,name') -> find();
		if(!$category){
			header("location:".U('backproductimgage', array('id' => $news['id'])));
		}
		$this->getImageByUrl('http://'.$domain[0].'/'.$this->siteid.'/'.$category['name'].'?vajax=1');
		echo '<script>window.location.href="'.U('backcategoryimgage', array('id' => $category['id'])).'";</script>';
	}
}